<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, viewport-fit=cover" />
		<title>填写信息</title>
		<meta name="viewport"
			content="initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, user-scalable=no">
		<link rel="stylesheet" type="text/css" href="css/info.css?id=2" />
		<link rel="stylesheet" type="text/css" href="css/every.css?id=2" />
	</head>
	<body>
		<!--  -->
		<div class="wrap">
			<div class="header">
				<span class="shu"></span>
				<b>请填写收货信息</b>
			</div>
			<div class="box disRow_S">
				<div class="tit">联系人</div>
				<input id="name" style="margin-left: 0.8rem;" />
			</div>
			<div class="box disRow_S">
				<div class="tit">手机号码</div>
				<input id="tel" style="margin-left: 0.5rem;" />
			</div>

			<div class="box disRow_S">
				<div class="tit">所在地区 </div>
				<input type="text" class="input" readonly id="mys" name="addr" value="" />
			</div>
			<div class="box disRow_S">
				<div class="tit">详细地址 </div>
				<input type="text" id="adds" class="input" placeholder="所在地街道、楼牌号等" />
			</div>
			<!-- <div class="box disRow_S">
				<div class="tit">推荐人手机号 </div>
				<input type="text" id="tel_tui" class="input" placeholder="" />
			</div> -->
			<input type="hidden" id="is" value="" />
			<input type="hidden" id="battery_id" value="" />
			<!--保存地址-->
		</div>
		<div class="footer" id="savebox">
			<div class="save">确认</div>
		</div>
	</body>
	<script src="js/tui.js"></script>
	<script src="js/jq.js"></script>
	<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
	<script>
		$(function() {
			//获取活动信息
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/GetIndexData",
				data: {
					userid_locked: getUrldata('locked')
				},
				dataType: "json",
				async: true,
				success: function(res) {
					if (res.result_code == 200) {
						$('#mys').val('' + res.city + '')
					}
				}
			})
		})

		var uid = getUrldata('locked')
		var types = getUrldata('types')
		var u = navigator.userAgent
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 //android终端
		$('.save').click(function() {
			var name = $('#name').val();
			var tel = $('#tel').val();
			var myAddrs = $('#mys').val();
			var adds = $('#adds').val();
			if (name == '' || tel == '' || myAddrs == '' || adds == '') {
				if (isWeiXin()) {
					(function() {
						window.alert = function(name) {
							var iframe = document.createElement("IFRAME");
							iframe.style.display = "none";
							iframe.setAttribute("src", 'data:text/plain');
							document.documentElement.appendChild(iframe);
							window.frames[0].window.alert(name);
							iframe.parentNode.removeChild(iframe);
						}
					})();
					alert('请将信息填写完整再提交');
				}
				if (isiOS) {
					//ios
					window.webkit.messageHandlers.showAlert.postMessage('请将信息填写完整再提交');
					return
				}
				if (isAndroid) {
					//Android
					window.TiantianCharge_Android.showAlert('请将信息填写完整再提交');
					return
				}
			}
			//开始验证
			if (/^1[3456789]\d{9}$/.test(tel) === false) {
				if (isWeiXin()) {
					(function() {
						window.alert = function(name) {
							var iframe = document.createElement("IFRAME");
							iframe.style.display = "none";
							iframe.setAttribute("src", 'data:text/plain');
							document.documentElement.appendChild(iframe);
							window.frames[0].window.alert(name);
							iframe.parentNode.removeChild(iframe);
						}
					})();
					alert('请输入正确的手机号码');
				}
				if (isiOS) {
					//ios
					window.webkit.messageHandlers.showAlert.postMessage('请输入正确的手机号码');
					return
				}
				if (isAndroid) {
					//Android
					window.TiantianCharge_Android.showAlert('请输入正确的手机号码');
					return
				}
			}

			var is = $('#is').val()
			if (is == 1) {
				window.location.href = 'pay_down.html?locked=' + uid
				return;
			}
			$(".save").css("pointer-events", "none");
			$(".save").css("background-color", "#aba7a7");
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/add_battery",
				dataType: "json",
				data: {
					userid_locked: uid,
					name: name,
					phone: tel,
					area: myAddrs,
					address: adds,
					type: types
				},
				success: function(res) {
					if (res.result_code == 200) {
						var battery_id = res.data.battery_id; //换购电池订单
						$("#is").attr("value", 1);
						$("#battery_id").attr("value", battery_id);
						window.location.href = 'pay_down.html?locked=' + uid;
					} else if(res.result_code == 6){
						//太原市
						var battery_id = res.data.battery_id; //换购电池订单
						$("#is").attr("value", 1);
						$("#battery_id").attr("value", battery_id);
						if (isWeiXin()) {
							(function() {
								window.alert = function(name) {
									var iframe = document.createElement("IFRAME");
									iframe.style.display = "none";
									iframe.setAttribute("src", 'data:text/plain');
									document.documentElement.appendChild(iframe);
									window.frames[0].window.alert(name);
									iframe.parentNode.removeChild(iframe);
								}
							})();
							alert('该地区暂无库存，敬请期待');
							$(".save").css("pointer-events", "auto");
							$(".save").css("background-color", "#D92735");
							window.location.href = 'index.html?Uid=' + uid;
						}
						if (isiOS) {
							//ios
							window.webkit.messageHandlers.showAlert.postMessage('该地区暂无库存，敬请期待');
							$(".save").css("pointer-events", "auto");
							$(".save").css("background-color", "#D92735");
							window.location.href = 'index.html?Uid=' + uid;
						}
						if (isAndroid) {
							//Android
							window.TiantianCharge_Android.showAlert('该地区暂无库存，敬请期待');
							$(".save").css("pointer-events", "auto");
							$(".save").css("background-color", "#D92735");
							window.location.href = 'index.html?Uid=' + uid;
						}
						
					}else {
						if (isWeiXin()) {
							(function() {
								window.alert = function(name) {
									var iframe = document.createElement("IFRAME");
									iframe.style.display = "none";
									iframe.setAttribute("src", 'data:text/plain');
									document.documentElement.appendChild(iframe);
									window.frames[0].window.alert(name);
									iframe.parentNode.removeChild(iframe);
								}
							})();
							alert('提交失败');
							$(".save").css("pointer-events", "auto");
							$(".save").css("background-color", "#D92735");
							return
						}
						if (isiOS) {
							//ios
							window.webkit.messageHandlers.showAlert.postMessage('提交失败');
							$(".save").css("pointer-events", "auto");
							$(".save").css("background-color", "#D92735");
							return
						}
						if (isAndroid) {
							//Android
							window.TiantianCharge_Android.showAlert('提交失败');
							$(".save").css("pointer-events", "auto");
							$(".save").css("background-color", "#D92735");
							return
						}
					}
				}
			})

		})

		//判断微信
		function isWeiXin() {
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				return true; // 是微信端
			} else {
				return false;
			}
		}
		//判断是否有订单
		setTimeout(function() {
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/user_order",
				data: {
					userid_locked: uid
				},
				dataType: "json",
				async: true,
				success: function(res) {
					if (res.result_code == 200) {
						var data = res.data;
						if (data == null) {
							return
						}
						if (data.course == 1) {
							$("#name").attr("value", data.name);
							$("#tel").attr("value", data.phone);
							$("#mys").attr("value", data.area);
							$("#adds").attr("value", data.address);
							$("#is").attr("value", data.course);
							$("#battery_id").attr("value", data.battery_id);
							document.getElementById("name").setAttribute("readOnly", true);
							document.getElementById("mys").setAttribute("readOnly", true);
							document.getElementById("tel").setAttribute("readOnly", true);
							document.getElementById("adds").setAttribute("readOnly", true);
						}
					}

				}
			});
		}, 50);

		function getUrldata(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return decodeURI(r[2]);
			return null;
		}
	</script>
</html>
